package mdoc.tp1;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import domain.Address;
import domain.Contact;
import domain.ContactGroup;
import domain.EmailValidator;
import domain.PhoneNumber;
import domain.DAO.DAOContact;
import domain.DAO.DAOContactGroup;
import domain.DAO.DAOFactory;
import domain.interfaces.IDAOContact;
import domain.interfaces.IDAOContactGroup;

/**
 * Servlet implementation class newContact
 */
public class AddContactFromSpringBean extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddContactFromSpringBean() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int numContacts = Integer.valueOf(request.getParameter("num_contacts"));
		WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
		for(int i=0; i < numContacts; i++ ){
			createContact(context);
		}
		response.sendRedirect("LoadAllContacts");
	}

	private void createContact(WebApplicationContext context) {
		Contact contactProperty = (Contact) context.getBean("contactProperty");
		Address addressProperty = (Address) context.getBean("addressProperty");
		DAOFactory.getDAOAddress(getServletContext()).create(addressProperty);
		contactProperty.setAdd(addressProperty);
    	DAOFactory.getDAOContact(getServletContext()).create(contactProperty);
	}
	
	public Set<PhoneNumber> createPhones(String home, String mobile, String work)
	{
		Set<PhoneNumber> setRes = new HashSet<PhoneNumber>();
		home = home.replaceAll("\\s", "");
		if (home != "")
		{
			PhoneNumber p = new PhoneNumber();
			p.setPhoneKind("Home");
			p.setPhoneNumber(home);
			setRes.add(p);
			System.out.println("home");
		}

		mobile = mobile.replaceAll("\\s", "");
		if (mobile != "")
		{
			PhoneNumber p = new PhoneNumber();
			p.setPhoneKind("Mobile");
			p.setPhoneNumber(mobile);
			setRes.add(p);
			System.out.println("mobile");
		}

		work = work.replaceAll("\\s", "");
		if (work != "")
		{
			PhoneNumber p = new PhoneNumber();
			p.setPhoneKind("Work");
			p.setPhoneNumber(work);
			setRes.add(p);
			System.out.println("work");
		}

		return setRes;
	}

}
